
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.Artifact;
import com.crv.ci.service.IArtifactService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class ArtifactAction extends AbstractAdminController<IArtifactService>{

    @Autowired
    private IArtifactService artifactService;
    
    @RequestMapping(value="/artifact/list")
    public String list(Artifact artifact,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(artifact); 	
    	BizData4Page<Artifact> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("artifact", artifact);
    	return "/module/artifact/list";
    }    
    
    @RequestMapping(value="/artifact/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("artifact", new Artifact());
        return "module/artifact/edit";
    }    
    
    @RequestMapping(value="/artifact/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	Artifact artifact = artifactService.fetch(id);
    	model.addAttribute("artifact", artifact);
        return "module/artifact/edit";
    }    
    
    @RequestMapping(value="/artifact/save")
    @ResponseBody
    public String save(HttpServletRequest request,Artifact artifact){
		boolean isAddNew = StringUtils.isBlank(artifact.getId())?true:false;
		if(isAddNew)
		{
			artifact.setId(IdGenerator.createNewId());
			artifactService.add(artifact);
		}
		else
		{
			artifactService.update(artifact);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/artifact/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	artifactService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(Artifact artifact)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("artType", artifact.getArtType());		
		conditions.put("name", artifact.getName());		
		conditions.put("path", artifact.getPath());		
		conditions.put("size", artifact.getSize());		
		conditions.put("createrId", artifact.getCreaterId());		
		conditions.put("createrName", artifact.getCreaterName());		
		conditions.put("updaterId", artifact.getUpdaterId());		
		conditions.put("updaterName", artifact.getUpdaterName());		
		conditions.put("createDate", artifact.getCreateDate());		
		conditions.put("updateDate", artifact.getUpdateDate());		
    	return conditions;
    }

    @Override
    protected IArtifactService getMainService() {
        return artifactService;
    }

    @Override
    protected String getMainObjName() {
        return "artifact";
    }

    @Override
    protected String getViewTitle() {
        return "artifact";
    }
}
